package com.itany.corejava.code07_常用类.其他类;

import com.itany.corejava.constant.DateutilConstant;
import com.itany.corejava.util.DateUtil;

import java.util.Calendar;
import java.util.Date;

/**
 * @author MissChen
 * @date 2024年03月02日 9:51
 */
public class Test01_日历类 {
    public static void main(String[] args) {
        Calendar calendar=Calendar.getInstance();
        Date date=calendar.getTime();
        System.out.println(date);
        System.out.println(DateUtil.dateToString(date));

        //setTime(Date date)
        calendar.setTime(DateUtil.getYesterday(new Date()));
        System.out.println(DateUtil.dateToString(calendar.getTime()));

        //当前时间的12小时后
        Date date2=new Date(new Date().getTime()+ DateutilConstant.ONE_DAY_MILLISECONDS/2);
        calendar.setTime(date2);

        //get()：根据传递的常量值的不同，返回日期对应部分的值
        System.out.println(calendar.get(Calendar.YEAR)+"年");

        //对于月份，取值0-11
        System.out.println(calendar.get(Calendar.MONTH)+1+"月");
        System.out.println(calendar.get(Calendar.DATE)+"日");
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH)+"日");

        //十二小时制
        System.out.println(calendar.get(Calendar.HOUR)+"时");

        //二十四小时制
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY)+"时");
        System.out.println(calendar.get(Calendar.MINUTE)+"分");
        System.out.println(calendar.get(Calendar.SECOND)+"秒");
        System.out.println(calendar.get(Calendar.MILLISECOND)+"毫秒");

        //设置为昨天
        calendar.set(calendar.DATE,1);
        calendar.set(calendar.HOUR_OF_DAY,11);
        //日期会自动进行运算，当设置的值超出了范围之后
        // 会在日期中自动进行运算
        // 月份取值0-11,13表示下一年的2月
        calendar.set(Calendar.MONTH,13);
        System.out.println(DateUtil.dateToString(calendar.getTime()));

        //根据某年某月获取对应的天数
        System.out.println(DateUtil.getDay(2023,2));

        Calendar calendar2=Calendar.getInstance();
        System.out.println(DateUtil.dateToString(calendar2.getTime()));

        //add
        calendar2.add(Calendar.YEAR,-1);//年份-1
        calendar2.add(Calendar.MONTH,1);//月份+1
        calendar2.add(Calendar.DATE,10);//日期+10
        calendar2.add(Calendar.HOUR_OF_DAY,20);//20小时

        System.out.println(DateUtil.dateToString(calendar2.getTime()));

        System.out.println("毫秒数:"+calendar2.getTimeInMillis());
        calendar2.clear();//清空日期,此时的日期会自动重置为纪元时间的日历
        System.out.println(calendar2.getTime());

        Calendar calendar3=Calendar.getInstance();
        System.out.println("年份可能的最大值:"+calendar3.getActualMaximum(Calendar.YEAR));
        System.out.println("月份可能的最大值:"+calendar3.getActualMaximum(Calendar.MONTH));
        System.out.println("天数可能的最大值:"+calendar3.getActualMaximum(Calendar.DATE));

        System.out.println(DateUtil.getDay2(2024,7));
    }
}
